package org.jwd.storage.controller;

import lombok.extern.slf4j.Slf4j;
import org.jwd.core.anno.AuditRecord;
import org.jwd.core.consts.OptType;
import org.jwd.core.entity.UserEntity;
import org.jwd.core.web.Res;
import org.jwd.storage.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author vizz
 * @since 2022/5/14 20:05
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("get-all-for-test")
    public List getAll() {
        return userService.getAll();
    }

    //增删改查

    /**
     * {
     * "userCode":"519test",
     * "nickname":"testhh",
     * "password":"111111"
     * }
     */
    @AuditRecord(optType = OptType.INSERT)
    @PostMapping("create")
    public Res create(@RequestBody UserEntity userEntity) {
        userService.save(userEntity);
        return Res.succeed("创建成功");
    }

    @PostMapping("create-batch")
    public Res createBatch(List<UserEntity> userEntities) {
        try {
            userService.saveBatch(userEntities);
        } catch (Exception e) {
            log.warn("====批量导入失败====" + e.getMessage());
            return Res.fail(400, "批量导入失败", e.getMessage());
        }
        return Res.succeed("批量导入成功");
    }

    @AuditRecord(optType = OptType.DELETE,paramIsId = true)
    @PostMapping("delete/{id}")
    public Res delete(@PathVariable String id) {
        userService.removeById(id);
        return Res.succeed("删除成功");
    }
}
